import dataPreferences  from '@ohos.data.preferences';
let context = getContext(this)
let preference: dataPreferences.Preferences
const PREF_NAME = 'shop_pref'

class PrefUtil{

    //创建Preference
    async getPreferenceFromStorage(){
          preference = await dataPreferences.getPreferences(context, PREF_NAME)
        }

    //保存数据
    async  putString(key:string, value: string) {
      if (!preference) {
        await this.getPreferenceFromStorage()
      }
      await preference.put(key, value)
      await preference.flush()
    }

    //获取数据
    async getString(key:string) {
      let data = ''
      if (!preference) {
        await this.getPreferenceFromStorage()
      }
      data = (await preference.get(key, '')).toString()
      return data
    }
}

export default new PrefUtil()
